<?php

/**
 * This is the model class for table "{{comfort}}".
 *
 * The followings are the available columns in table '{{comfort}}':
 * @property integer $id
 * @property string $title
 * @property string $icon
 * @property integer $is_active
 */
class Comfort extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Comfort the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{comfort}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, is_active', 'required'),
            array('is_active', 'numerical', 'integerOnly' => true),
            array('title', 'length', 'max' => 254),
            array('icon', 'file', 'allowEmpty' => true, 'types' => 'jpg, gif, png'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, icon, is_active', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'title' => 'Title',
            'icon' => 'Icon',
            'is_active' => 'Is Active',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('icon', $this->icon, true);
        $criteria->compare('is_active', $this->is_active);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function isActive() {
        return $this->is_active ? 'Активна' : 'Не aктивна';
    }

    public static function updateComfortInModel($model) {
        
        $flatsVsComfort = FlatsVsComfort::model()->findAll('flats_id=:flats_id', array(':flats_id' => $model->id));
        
        if (!empty($flatsVsComfort)) {
            foreach ($flatsVsComfort as $item) {
                $item->delete();
            }
        }
        
        if (!empty($model->comfort)) {
            foreach ($model->comfort as $comfortId) {
                if ($comfortId != 0) {
                    $flatsVsComfort = new FlatsVsComfort();
                    $flatsVsComfort->flats_id = $model->id;
                    $flatsVsComfort->comfort_id = $comfortId;

                    $flatsVsComfort->save();
                }
            }
        }
    }
    
    public static function getActiveComfort() {
        return Comfort::model()->findAll('is_active=:is_active', array(':is_active' => '1'));
    }

}